//
//  DYSettingViewController.m
//  BaiSi
//
//  Created by mac on 16/10/15.
//  Copyright © 2016年 DeYi. All rights reserved.
//

#import "DYSettingViewController.h"

static NSString * const ID = @"settingCell";

@interface DYSettingViewController ()

@end

@implementation DYSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.tableFooterView = [[UIView alloc] init];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
    //获取cache下文件夹大小
    NSInteger totalSize = [self getDirectorySizeAtPath:LHCachePath];
    //转为字符串
    NSString *str = [self getSizeStr:totalSize];
    //赋值
   cell.textLabel.text = str;
    
    return cell;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //清除cache内缓存
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSString *message = cell.textLabel.text;
    [self popViewToClearDiskMemoryAtPath:LHCachePath withMessage:message];
    
    
}

#pragma mark - 弹出弹窗再次确认是否删除缓存
- (void)popViewToClearDiskMemoryAtPath:(NSString *)path withMessage:(NSString *)message {
    //创建弹窗
    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"清除缓存" message:message preferredStyle:UIAlertControllerStyleAlert];
    //取消按钮
    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    //确定按钮
    UIAlertAction *sureActon = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [self clearDiskMemoryAtPath:path];
        [self.tableView reloadData];
        [self dismissViewControllerAnimated:YES completion:^{
            
        }];
    }];
    //添加按钮
    [alertVc addAction:cancleAction];
    [alertVc addAction:sureActon];
    //弹出窗口
    [self presentViewController:alertVc animated:YES completion:nil];
}
#pragma mark - 清除缓存
- (void)clearDiskMemoryAtPath:(NSString *)path {
    //删除文件夹
    [[NSFileManager defaultManager]removeItemAtPath:path error:nil];
    //创建空cache的文件夹
    [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
    
}

#pragma mark - 得到文件夹大小字符串
-(NSString *)getSizeStr:(NSInteger)totalSize {
    NSString *str = @"清除缓存";
    if (totalSize > 1000 *1000 ) {
        str = [NSString stringWithFormat:@"%@(%0.1fMB)",str,totalSize / 1000.0 /1000.0];
    } else if (totalSize > 1000) {
         str = [NSString stringWithFormat:@"%@(%0.1fKB)",str,totalSize / 1000.0];
    } else if (totalSize > 0) {
        str = [NSString stringWithFormat:@"%@(%ldB)",str,totalSize];
    }
    str = [str stringByReplacingOccurrencesOfString:@".0" withString:@""];
    return str;
}

#pragma mark - 计算文件夹的大小
- (NSInteger)getDirectorySizeAtPath:(NSString *)path {
   
    
    //文件管理者
    NSFileManager *mgr = [NSFileManager defaultManager];
    //获取cache子路径数组
    NSArray *subpaths = [mgr subpathsAtPath:path];
    NSInteger totalSize = 0;
    //遍历计算
    for (NSString *subpath in subpaths) {
        //拼接全路径
        NSString *subStr = [path stringByAppendingPathComponent:subpath];
        //排除系统隐藏文件和文件夹
        BOOL isDirectory;
        BOOL isExist = [mgr fileExistsAtPath:subStr isDirectory:&isDirectory];
        if (isDirectory) continue;//文件夹
        if([subStr containsString:@".DS_Store"]) continue ;
        NSDictionary *dic = [mgr attributesOfItemAtPath:subStr error:nil];
        totalSize += [dic fileSize];
    }
    return totalSize;
}
@end
